"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const components_1 = require("../../../helpers/components");
const member_1 = require("../../../logic_v2/services/member/member");
const template_service_1 = require("../../../logic_v2/services/ui/template_service");
const user_1 = require("../../../logic_v2/services/user");
const error_1 = require("../../../utils/error");
const modal_1 = require("../../../utils/modal");
const auto_register_1 = __importDefault(require("../../../behaviors/auto_register"));
const activity_1 = require("../../../logic_v2/services/activity/activity");
const activity_2 = require("../../../logic_v2/adapters/activity");
const activity_3 = require("../../../helpers/constants/activity");
const userService = new user_1.UserService();
const templateService = new template_service_1.TemplateService();
const memberService = new member_1.MemberService();
const activityService = new activity_1.ActivityService();
let timer;
(0, components_1.DefineComponent)({
    behaviors: [(0, auto_register_1.default)()],
    data: {
        sid: '',
        cardId: '',
        avatar: '',
        cardNo: '',
        qrcode: '',
        barcode: '',
        cardTypeName: '',
        refresh: 30,
        showReceived: false,
        receivedReward: {},
        logo: '',
        receivedData: {},
        showMemberRegister: false
    },
    computed: {
        maxHeight(data) {
            // 除了滚动区域的高度 100上边距+54上padding+22标题+24描述+16coupon上margin+88按钮区域
            let otherHeight = 56 + 56 + 22 + 24 + 16 + 88;
            const receivedData = data.receivedData;
            if (receivedData.received) {
                otherHeight += data.screenHeight * 0.3;
            }
            else {
                otherHeight += 100;
            }
            if (receivedData.reward && receivedData.reward.length > 0) {
                otherHeight += 44;
            }
            if (receivedData.multiReward && receivedData.multiReward.length > 0) {
                otherHeight += 50;
            }
            return `max-height: calc(100vh - ${otherHeight * 2}rpx);`;
        },
        cover(data) {
            const receivedData = data.receivedData;
            return [activity_3.ActivityType.selfRegister, activity_3.ActivityType.invitedRegister, activity_3.ActivityType.advertisingRegister].includes(receivedData.type) ? '' : data.goodsCover;
        }
    },
    methods: {
        onLoad(options) {
            this.setData({
                cardId: options.cardId,
                sid: options.sid
            });
            this.getUserInfo();
            this.getQrcode();
            // this.setTimer()
        },
        onUnload() {
            clearInterval(timer);
        },
        clearTimer() {
            if (timer) {
                clearInterval(timer);
            }
        },
        setTimer() {
            this.clearTimer();
            timer = setInterval(() => {
                this.getQrcode();
            }, (this.data.refresh || 30) * 1000);
        },
        getUserInfo() {
            const avatar = userService.getAvatar();
            const config = templateService.getEmptyConfigByKey('avatar');
            const brandInfo = templateService.getGlobalBrandInfo();
            this.setData({
                avatar: avatar || config?.image || '',
                logo: brandInfo?.logo || ''
            });
        },
        async tapGetQrcode() {
            this.clearTimer();
            await this.getQrcode();
            this.setTimer();
        },
        async getQrcode() {
            const { cardId } = this.data;
            if (cardId) {
                this.getCardQrcode();
            }
            else {
                this.getMemberQrcode();
            }
        },
        async getCardQrcode() {
            const { cardId } = this.data;
            if (cardId) {
                // 会员卡的会员码
                try {
                    const { cardNo, qrcode, barcode, cardTypeName, refresh } = await memberService.getCardQrCode(parseInt(cardId));
                    this.setData({
                        cardNo,
                        cardTypeName,
                        refresh,
                        qrcode: 'data:image/png;base64,' + qrcode,
                        barcode: 'data:image/png;base64,' + barcode
                    });
                }
                catch (err) {
                    if (err instanceof error_1.AppError) {
                        (0, modal_1.infoModal)({ title: '提示', content: err.msg });
                    }
                }
            }
        },
        authPhoneNumber(e) {
            const { isSuccess } = e.detail;
            if (isSuccess) {
                this.setData({
                    showMemberRegister: false
                });
                this.getMemberQrcode();
            }
        },
        async getMemberQrcode() {
            const { sid } = this.data;
            try {
                const isVip = userService.isVIP();
                if (isVip) {
                    const { code, qrcode, barcode, refresh } = await memberService.getMemberQrCode();
                    this.setData({
                        cardNo: code,
                        refresh,
                        qrcode: 'data:image/png;base64,' + qrcode,
                        barcode: 'data:image/png;base64,' + barcode
                    });
                    console.debug('fea/sid', sid);
                    if (parseInt(sid || '0') > 0) {
                        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
                        // @ts-ignore
                        this.autoRegShopProcess(true, sid);
                    }
                }
                else {
                    this.setData({
                        showMemberRegister: true
                    });
                    const couponTemplate = await activityService.getCouponListByRegister({
                        fromAd: 2,
                        fromUid: 0
                    });
                    const receive = activity_2.ActivityAdapter.genCouponAddContextByRegister(couponTemplate, {
                        show: true,
                        showType: 1,
                        content: {
                            firstRow: '',
                            secondRow: '',
                            button: ''
                        },
                        contextType: 1,
                        textMode: 1,
                        bgImg: '',
                        url: '',
                        width: 0,
                        height: 0,
                        existsVipGoods: true
                    });
                    receive.reward.title = '您暂无会员码, 请先注册';
                    receive.reward.tips = `注册后, ${receive.reward.tips || '享受会员价'}`;
                    receive.reward.buttons = [
                        { theme: activity_3.ReceiveCouponButtonTheme.auth, text: '立即注册 领红包', action: activity_3.ReceiveActionType.checkCoupon }
                    ];
                    this.setData({
                        receivedData: receive.reward
                    });
                }
            }
            catch (err) {
                if (err instanceof error_1.AppError) {
                    (0, modal_1.infoModal)({ title: '提示', content: err.msg });
                }
            }
        }
    }
});
